package com.tuling.proxy;

import com.tuling.proxy.advice.AfterMethodInterceptor;
import com.tuling.proxy.advice.BeforeMethodInterceptor;
import com.tuling.proxy.advice.MethodAroundInterceptor;
import com.tuling.proxy.advice.ThrowAroundInterceptor;
import org.springframework.aop.framework.ProxyFactory;

public class ProxyFactoryTest {
    public static void main(String[] args) {
        //判断使用jdk 还是cglib 条件	(config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config))
        UserServiceInterface userServiceInterface = new UserServiceImpl();
        ProxyFactory proxyFactory = new ProxyFactory();
        //底层通过target 包装成SingletonTargetSource targetsource
        proxyFactory.setTarget(userServiceInterface);
        proxyFactory.setInterfaces(UserServiceInterface.class);
        proxyFactory.setProxyTargetClass(true);
        //添加advice 切面逻辑将advice 转换成advisor
        proxyFactory.addAdvice(new MethodAroundInterceptor());
        proxyFactory.addAdvice(new BeforeMethodInterceptor());
        proxyFactory.addAdvice(new AfterMethodInterceptor());
        proxyFactory.addAdvice(new ThrowAroundInterceptor());
        UserServiceInterface proxy = (UserServiceInterface)proxyFactory.getProxy();
        //当执行道理对象的时候会执行invoke 方法
        proxy.test();

    }
}
